//
//  CategoryTextField.swift
//  MusicApp
//
//  Created by tianjs on 2023/4/23.
//

import SwiftUI

//TextView

struct CategoryTextField: View {
    @State private var numbers: String = ""
    @FocusState private var focusedField: Bool
    
    var body: some View {
        Form {
            TextField(text: $numbers, prompt: Text("数量")) {
                
            }
            .textFieldStyle(.automatic)
            .speechAdjustedPitch(0.35)
            .keyboardType(.decimalPad)
            // 绑定 focusedField
            .focused($focusedField, equals: true)
            // 键盘添加toolbar
            .toolbar {
                ToolbarItem(placement: .keyboard, content: {
                    Spacer()
                })
                ToolbarItem(placement: .keyboard) {
                    Button {
                        self.focusedField = false
                    } label: {
                        Image(systemName: "keyboard.chevron.compact.down")
                    }
                }
            }
            .padding(.vertical)
            .onAppear {
                // 通过设置 UITextField 中的 clearButtonMode 来实现效果
                // .whileEditing  编辑时出现清除按钮
                // .unlessEditing  编辑时不出现，编辑后才出现清除按钮
                // .always  一直显示清除按钮
                UITextField.appearance().clearButtonMode = .whileEditing
            }
        }
    }
}

struct CategoryTextField_Previews: PreviewProvider {
    static var previews: some View {
        CategoryTextField()
    }
}
